<?php

/**
 * This is the model class for table "ad_eblast".
 *
 * The followings are the available columns in table 'ad_eblast':
 * @property integer $id
 * @property integer $ad_id
 * @property string $advertisement
 * @property integer $main_category
 * @property string $start_date
 * @property string $end_date
 */
class Eblast extends CActiveRecord {

    public $otherCategoryIds = array();
    public $email = '';

    const STATUS_PENDING = 0;
    const STATUS_APPROVE = 1;
    const STATUS_DENY = 2;
    const STATUS_PAID = 3;

    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return Eblast the static model class
     */
    public static function model($className = __CLASS__) {
	return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
	return '{{ad_eblast}}';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
	// NOTE: you should only define rules for those attributes that
	// will receive user inputs.
	return array(
	    array('main_category', 'required'),
	    array('ad_id, main_category', 'numerical', 'integerOnly' => true),
	    array('advertisement', 'length', 'max' => 255),
	    array('start_date, end_date, otherCategoryIds, email, status', 'safe'),
	    // The following rule is used by search().
	    // Please remove those attributes that should not be searched.
	    array('id, ad_id, advertisement, main_category, start_date, end_date', 'safe', 'on' => 'search'),
	);
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
	// NOTE: you may need to adjust the relation name and the related
	// class name for the relations automatically generated below.
	return array(
	    'ad' => array(self::BELONGS_TO, 'Ad', 'ad_id'),
	    'mainCategory' => array(self::BELONGS_TO, 'Category', 'main_category'),
	    'categories' => array(self::MANY_MANY, 'Category', 'ad_eblast_categories(eblast_id, category_id)')
	);
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
	return array(
	    'id' => 'ID',
	    'ad_id' => 'Ad',
	    'advertisement' => 'Advertisement',
	    'main_category' => 'Main Category',
	    'start_date' => 'Pick Date',
	    'end_date' => 'End Date',
	);
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search() {
	// Warning: Please modify the following code to remove attributes that
	// should not be searched.

	$criteria = new CDbCriteria;
//	$criteria->with = array("ad");
//	$criteria->alias = 't';
	$criteria->compare('id', $this->id);
	$criteria->compare('ad_id', $this->ad_id);
	$criteria->compare('main_category', $this->main_category);
	$criteria->compare('status', $this->status);
	return new CActiveDataProvider($this, array(
	    'criteria' => $criteria,
	));
    }

    /**
     * 
     */
    public function afterSave() {
	parent::afterSave();

	//update other categorie
	if (!empty($this->otherCategoryIds)) {
	    //
	    $db = db();
	    $db->createCommand()->delete('{{ad_eblast_categories}}', 'eblast_id = ' . $this->id);
	    //insert new row
	    foreach ($this->otherCategoryIds as $catId) {
		$db->createCommand()->insert('{{ad_eblast_categories}}', array(
		    'eblast_id' => $this->id,
		    'category_id' => $catId
		));
	    }
	}
    }

    public function afterDelete() {
	parent::afterDelete();
	//delete file
	if ($this->advertisement) {
	    deleteFile(UPLOAD_PATH . 'eblast/' . $this->advertisement);
	}
	$db = db();
	$db->createCommand()->delete('{{ad_eblast_categories}}', 'eblast_id = ' . $this->id);
	//delete file
	if ($this->advertisement) {
	    if (file_exists(UPLOAD_PATH . 'eblast/' . $this->advertisement)) {
		unlink(UPLOAD_PATH . 'eblast/' . $this->advertisement);
	    }
	}
    }

    /*
     * get file url
     */

    public function getFileUrl() {
	if ($this->advertisement) {
	    return "<a target='_blank' href='" . UPLOAD_URL . "eblast/" . $this->advertisement . "'>Link</a>";
	}
	return "";
    }

    public static function getAllStatus() {
	return array(
	    0 => 'Pending',
	    1 => 'Approved',
	    2 => 'Denied',
	    3 => 'Paid'
	);
    }

    public function getStatusText() {
	switch ($this->status) {
	    case self::STATUS_APPROVE : return 'Approved';
	    case self::STATUS_DENY : return 'Denied';
	    case self::STATUS_PENDING : return 'Pending';
	    default : return 'Paid';
	}
    }

}

